/* 
 * File:   MPISlave.cpp
 * Author: alexander
 * 
 * Created on July 28, 2011, 5:17 PM
 */

#include "MPISlave.h"

MPISlave::MPISlave(int rank, Funcao *func, LBFGS *lbfgs, double maxiters, int type, int n, int m, int info, double epsg, double epsf, double epsx, int maxEvals, ap::real_1d_array xBFGS, MGrasp *mgrasp) {
    this->m = m;
    this->n = n;
    this->maxEvals = maxEvals;
    this->mgrasp = mgrasp;
    this->type = type;
    this->rank = rank;
    this->xBFGS = xBFGS;
    this->info = info;
    this->epsf = epsf;
    this->epsg = epsg;
    this->epsx = epsx;
    this->func = func;
    this->lbfgs = lbfgs;
    this->maxiters = maxiters;



}

MPISlave::MPISlave(const MPISlave& orig) {
}

MPISlave::~MPISlave() {
}

void MPISlave::start() {
    bool sucess = false;
    if (MPIBasics::DEBUG_SLAVE) {
        printf("Slave (%d) - Inicializado... \n", rank);
    }

    if (type == PURO) {
        if (MPIBasics::DEBUG_SLAVE) {
            printf("Slave(%d) Puro... \n", rank);
        }
        sucess = mgrasp->startParallel(false, m, maxEvals, rank);

    } else if (type == HIBRIDO) {
        printf("[%d] Hibrido... \n", rank);
        sucess = mgrasp->startParallel(true, m, maxEvals, rank);
    } else {
        printf("Ainda não implementado\n");
        exit(1);
        printf("[%d] BFGS... \n", rank);
        x = new double[n];
        mgrasp->unifRandom(x);
        xBFGS.setbounds(1, n);
        for (int j = 0; j < n; j++) {
            xBFGS(j + 1) = x[j];
        }
        lbfgs = new LBFGS(func, false);
        lbfgs->minimize(n, m, xBFGS, epsg, epsf, epsx, maxiters, info);

        sucess = true;
        printf("[%d] Info = %d \n", rank, info);

    }

    if (MPIBasics::DEBUG_SLAVE) {
        printf("Slave (%d) - Finalizado\n", rank);
    }
}
